package cc.idoone.jasperreport.test;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.export.JRXlsExporter;

/**
 * jrxml 文档编译保存为 jasper 文档测试
 * 
 * @author hemj
 * @date 2017年2月14日
 */
public class JasperReportSection {

	public static void main(String[] args) {
		String sourceFileName = "samples/jrtemplates/jasper_report_section.xml";

		System.out.println("Compiling Report Design ...");
		try {
			// 将会在同目录下生成同名且扩展名为 jasper 的文档
			// Compile the report to a file name same as the JRXML file name
			String jasper = JasperCompileManager.compileReportToFile(sourceFileName);
			String jrprint = JasperFillManager.fillReportToFile(jasper, null,
					new JREmptyDataSource());
			if (jrprint != null) {
				// 1- export to PDF
				JasperExportManager.exportReportToPdfFile(jrprint, "E://sample_report.pdf");

				// 2- export to HTML
				JasperExportManager.exportReportToHtmlFile(jrprint, "E://sample_report.html");

				// 3- export to Excel sheet
				JRXlsExporter exporter = new JRXlsExporter();
				exporter.setParameter(JRExporterParameter.INPUT_FILE_NAME, jrprint);
				exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "E://sample_report.xls");
				exporter.exportReport();
			}
		} catch (JRException e) {
			e.printStackTrace();
		}
		System.out.println("Done compiling!!! ...");
	}
}
